package com.lc.app.myaccount.dao;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.lc.app.myaccount.entity.InAccount;
import com.lc.app.myaccount.helper.DBOpenHelper;

/**
 * Created by LC on 2/5/2016.
 */
public class InAccountDao {
    private DBOpenHelper helper;
    private SQLiteDatabase db;

    public InAccountDao(Context context) {
        this.helper = new DBOpenHelper(context);
        db = helper.getWritableDatabase();
    }

    public void add(InAccount inAccount) {
        db.execSQL("insert into tb_in_account(money,time,type,handler,mark,user_id)", inAccount.toAddArray());
    }

    public void update(InAccount inAccount) {
        db.execSQL("update tb_in_account set money=?,time=?,type=?,handler=?,mark=?,user_id = ? where _id = ?", inAccount.toUpdateArray());
    }

    public InAccount find(int id) {
        Cursor cursor = db.rawQuery("select _id,money,time,type,handler,mark,user_id from tb_in_account where _id = ?", new String[]{String.valueOf(id)});
        if (cursor.moveToNext()) {
            InAccount inAccount = new InAccount();
            inAccount.set_id(cursor.getInt(cursor.getColumnIndex("_id")));
            inAccount.setMoney(cursor.getDouble(cursor.getColumnIndex("money")));
            inAccount.setTime(cursor.getString(cursor.getColumnIndex("time")));
            inAccount.setType(cursor.getString(cursor.getColumnIndex("type")));
            inAccount.setHandler(cursor.getString(cursor.getColumnIndex("handler")));
            inAccount.setMark(cursor.getString(cursor.getColumnIndex("mark")));
            inAccount.setUserId(cursor.getInt(cursor.getColumnIndex("user_id")));

            return inAccount;
        } else {
            return null;
        }
    }
}
